#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash wham-1.8.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget numpy python2-scipy

set -e
wget -t 20 -c -P $buildpath https://github.com/zeeev/wham/archive/refs/tags/1.8.0.zip
wget -t 40 -c -P $buildpath https://github.com/mengyao/Complete-Striped-Smith-Waterman-Library/archive/8c9933a1685e0ab50c7d8b7926c9068bc0c9d7d2.zip
wget -t 40 -c -P $buildpath https://github.com/pezmaster31/bamtools/archive/2d7685d2aeedd11c46ad3bd67886d9ed65c30f3e.zip
wget -t 40 -c -P $buildpath https://github.com/ekg/fastahack/archive/0a8ca3de9239b061839418b306bb6a9072990158.zip
wget -t 40 -c -P $buildpath https://github.com/seqan/seqan/archive/7f3ca44fbe777f3b637ff57ccc1dcd975cca1661.zip

unzip -d $buildpath "$buildpath"/1.8.0.zip && rm -rf "$buildpath"/1.8.0.zip
unzip -d $buildpath "$buildpath"/0a8ca3de9239b061839418b306bb6a9072990158.zip && rm -rf "$buildpath"/0a8ca3de9239b061839418b306bb6a9072990158.zip
unzip -d $buildpath "$buildpath"/2d7685d2aeedd11c46ad3bd67886d9ed65c30f3e.zip && rm -rf "$buildpath"/2d7685d2aeedd11c46ad3bd67886d9ed65c30f3e.zip
unzip -d $buildpath "$buildpath"/7f3ca44fbe777f3b637ff57ccc1dcd975cca1661.zip && rm -rf "$buildpath"/7f3ca44fbe777f3b637ff57ccc1dcd975cca1661.zip
unzip -d $buildpath "$buildpath"/8c9933a1685e0ab50c7d8b7926c9068bc0c9d7d2.zip && rm -rf "$buildpath"/8c9933a1685e0ab50c7d8b7926c9068bc0c9d7d2.zip

cp -rf "$buildpath"/Complete-Striped-Smith-Waterman-Library-8c9933a1685e0ab50c7d8b7926c9068bc0c9d7d2/* "$buildpath"/wham-1.8.0/src/Complete-Striped-Smith-Waterman-Library/ && rm -rf "$buildpath"/Complete-Striped-Smith-Waterman-Library-8c9933a1685e0ab50c7d8b7926c9068bc0c9d7d2
cp -rf "$buildpath"/bamtools-2d7685d2aeedd11c46ad3bd67886d9ed65c30f3e/* "$buildpath"/wham-1.8.0/src/bamtools/ && rm -rf "$buildpath"/bamtools-2d7685d2aeedd11c46ad3bd67886d9ed65c30f3e
cp -rf "$buildpath"/fastahack-0a8ca3de9239b061839418b306bb6a9072990158/* "$buildpath"/wham-1.8.0/src/fastahack/ && rm -rf "$buildpath"/fastahack-0a8ca3de9239b061839418b306bb6a9072990158
cp -rf "$buildpath"/seqan-7f3ca44fbe777f3b637ff57ccc1dcd975cca1661/* "$buildpath"/wham-1.8.0/src/seqan/ && rm -rf "$buildpath"/seqan-7f3ca44fbe777f3b637ff57ccc1dcd975cca1661
cp "$buildpath"/sse2neon-master/sse2neon.h "$buildpath"/wham-1.8.0/src/lib/
cp "$buildpath"/sse2neon-master/sse2neon.h "$buildpath"/wham-1.8.0/src/Complete-Striped-Smith-Waterman-Library/src/ && rm -rf "$buildpath"/sse2neon-master
cp wham-1.8.0.patch "$buildpath"

cd "$buildpath"
patch -f -p0 < wham-1.8.0.patch && rm -rf "$buildpath"/wham-1.8.0.patch
cd wham-1.8.0
make

mkdir -p "$installpath"/wham-1.8.0 && cp -r "$buildpath"/wham-1.8.0/* "$installpath"/wham-1.8.0
cat>"$installpath/wham-1.8.0/wham_modulefiles"<<EOF
#%Module1.0
conflict wham
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set WHAM \$pwd
setenv WHAM \$WHAM
prepend-path PATH \$WHAM/bin
EOF

rm -rf "$buildpath"/wham-1.8.0
